package com.xoa.service.attendance.schedule.impl;

import com.xoa.dao.attend.AttendSetMapper;
import com.xoa.dao.attendance.AttendScheduleMapper;
import com.xoa.dao.attendance.UserDutyMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.attend.AttendSet;
import com.xoa.model.attendance.AttendScheduleWithBLOBs;
import com.xoa.model.attendance.UserDuty;
import com.xoa.model.users.Users;
import com.xoa.service.attendance.schedule.AttendScheduleService;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.attend.AttendDateUtil;
import com.xoa.util.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AttendScheduleServiceImpl implements AttendScheduleService {

    @Autowired
    private AttendScheduleMapper attendScheduleMapper;

    @Autowired
    private UserDutyMapper userDutyMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private AttendSetMapper attendSetMapper;
    /**
     * 获取主页数据
     */
    @Override
    public ToJson getAttendSchedule() {
        ToJson toJson = new ToJson(1, "err");
        List<AttendScheduleWithBLOBs> attendScheduleWithBLOBsList = attendScheduleMapper.selectAttendScheduleList();
        try {
            if (attendScheduleWithBLOBsList != null && attendScheduleWithBLOBsList.size() > 0) {
                //添加name（部门，角色，用户）
                for (AttendScheduleWithBLOBs attendScheduleWithBLOBs : attendScheduleWithBLOBsList) {
                    getdeptNameAndPrivNameAndUserName(attendScheduleWithBLOBs);
                }
                toJson.setFlag(0);
                toJson.setMsg("ok");
                toJson.setObj(attendScheduleWithBLOBsList);
            } else {
                toJson.setFlag(0);
                toJson.setMsg("数据为空");
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            return toJson;
        }
        return toJson;
    }

    /**
     * 新增排班
     */
    @Override
    public ToJson insertAttendSchedule(AttendScheduleWithBLOBs attendScheduleWithBLOBs) {
        ToJson toJson = new ToJson(1, "err");
        try {
            if (StringUtils.checkNull(attendScheduleWithBLOBs.getDeptId()) &&
                    StringUtils.checkNull(attendScheduleWithBLOBs.getPrivId()) &&
                    StringUtils.checkNull(attendScheduleWithBLOBs.getUserId())) {
                toJson.setMsg("请添加排班人员");
            } else {
                int result = attendScheduleMapper.insertSelective(attendScheduleWithBLOBs);
                if (result >= 1) {
                    toJson.setFlag(0);
                    toJson.setMsg("新增成功");
                }
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return toJson;
    }

    /**
     * 获取编辑数据
     *
     * @param id
     * @return
     */
    @Override
    public ToJson getAttendScheduleById(Integer id) {
        ToJson toJson = new ToJson(0, "err");
        try {
            AttendScheduleWithBLOBs attendScheduleWithBLOBs = attendScheduleMapper.selectByPrimaryKey(id);
            if (attendScheduleWithBLOBs != null) {
                getdeptNameAndPrivNameAndUserName(attendScheduleWithBLOBs);
                toJson.setFlag(0);
                toJson.setMsg("ok");
                toJson.setObject(attendScheduleWithBLOBs);
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 获取id对应的名字
     */
    public void getdeptNameAndPrivNameAndUserName(AttendScheduleWithBLOBs attendScheduleWithBLOBs) {
        int flag = 0;
        if (attendScheduleWithBLOBs != null) {
            //获取部门的name
            if (attendScheduleWithBLOBs.getDeptId() != null && attendScheduleWithBLOBs.getDeptId().length() > 0) {
                if (!attendScheduleWithBLOBs.getDeptId().equals("ALL_DEPT")) {
                    List<String> deptNames = attendScheduleMapper.getDeptNames(attendScheduleWithBLOBs.getDeptId());
                    if (deptNames != null && deptNames.size() > 0) {
                        StringBuffer deptName = new StringBuffer();
                        for (int i = 0; i < deptNames.size(); i++) {
                            deptName.append(deptNames.get(i) + ",");
                        }
                        attendScheduleWithBLOBs.setDeptName(deptName.toString());
                    }
                } else {
                    attendScheduleWithBLOBs.setDeptName("全部部门");
                    flag = 1;
                }
            }

            //获取角色的name
            if (attendScheduleWithBLOBs.getPrivId() != null && attendScheduleWithBLOBs.getPrivId().length() > 0) {
                List<String> privNames = attendScheduleMapper.getprivNames(attendScheduleWithBLOBs.getPrivId());
                if (privNames != null && privNames.size() > 0) {
                    StringBuffer privName = new StringBuffer();
                    for (int i = 0; i < privNames.size(); i++) {
                        privName.append(privNames.get(i) + ",");
                    }
                    attendScheduleWithBLOBs.setPrivName(privName.toString());
                }
            }
            //获取用户的name
            if (attendScheduleWithBLOBs.getUserId() != null && attendScheduleWithBLOBs.getUserId().length() > 0) {
                List<String> userNames = attendScheduleMapper.getuserNames(attendScheduleWithBLOBs.getUserId());
                if (userNames != null && userNames.size() > 0) {
                    StringBuffer userName = new StringBuffer();
                    for (int i = 0; i < userNames.size(); i++) {
                        userName.append(userNames.get(i) + ",");
                    }
                    attendScheduleWithBLOBs.setUserName(userName.toString());
                }
            }
        }

        StringBuffer stringBuffer = new StringBuffer();
        if (attendScheduleWithBLOBs.getDeptName() != null) {
            stringBuffer.append(attendScheduleWithBLOBs.getDeptName());
        }
        if (flag != 1) {
            if (attendScheduleWithBLOBs.getPrivName() != null) {
                stringBuffer.append(attendScheduleWithBLOBs.getPrivName());
            }
            if (attendScheduleWithBLOBs.getUserName() != null) {
                stringBuffer.append(attendScheduleWithBLOBs.getUserName());
            }
        }
        attendScheduleWithBLOBs.setUseUsers(stringBuffer.toString());
    }

    /**
     * 编辑
     */
    @Override
    public ToJson updateAttendSchedule(AttendScheduleWithBLOBs attendScheduleWithBLOBs) {
        ToJson toJson = new ToJson(1, "err");
        try {
            if (StringUtils.checkNull(attendScheduleWithBLOBs.getDeptId()) &&
                    StringUtils.checkNull(attendScheduleWithBLOBs.getPrivId()) &&
                    StringUtils.checkNull(attendScheduleWithBLOBs.getUserId())) {
                toJson.setMsg("请添加排班人员");
            } else {
                int result = attendScheduleMapper.updateByPrimaryKeySelective(attendScheduleWithBLOBs);
                if (result >= 1) {
                    toJson.setFlag(0);
                    toJson.setMsg("修改成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 删除
     */
    @Override
    public ToJson deleteAttendSchedule(Integer asid) {
        ToJson toJson = new ToJson(1, "err");
        try {
            AttendScheduleWithBLOBs attendScheduleWithBLOBs = attendScheduleMapper.selectByPrimaryKey(asid);
            if (attendScheduleWithBLOBs.getStatus() == 0) {
                int result = attendScheduleMapper.deleteByPrimaryKey(asid);
                if (result >= 1) {
                    toJson.setFlag(0);
                    toJson.setMsg("删除成功");
                }
            } else {
                toJson.setMsg("未停用");
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return toJson;
    }

    /**
     * 停用启用0：停用（1：启用）
     *
     * @return
     */
    @Override
    public ToJson updateAttendScheduleStatus(Integer asid) {
        ToJson toJson = new ToJson(1, "err");
        try {
            AttendScheduleWithBLOBs attendScheduleWithBLOBs = attendScheduleMapper.selectByPrimaryKey(asid);
            //当前为启用状态就停用
            if (attendScheduleWithBLOBs.getStatus() == 1) {
                out(attendScheduleWithBLOBs);
                int result = attendScheduleMapper.updateAttendScheduleStatus(asid, 0);
                if (result >= 1) {
                    toJson.setFlag(0);
                    toJson.setMsg("停用成功");
                }
                //当前为停用状态就启用
            } else if (attendScheduleWithBLOBs.getStatus() == 0) {
                if (attendScheduleWithBLOBs.getDateStart() != null && attendScheduleWithBLOBs.getDateEnd() != null) {
                    if (isClash(attendScheduleWithBLOBs)) {
                        toJson.setMsg("排班数据人员有冲突");
                    } else {
                        this.start(attendScheduleWithBLOBs, 1);
                        int result = attendScheduleMapper.updateAttendScheduleStatus(asid, 1);
                        if (result >= 1) {
                            toJson.setFlag(0);
                            toJson.setMsg("启用成功");
                        }
                    }
                } else {
                    toJson.setMsg("排班时间为空");
                }
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return toJson;
    }

    /**
     * 停用(删除用户排班表相应数据)
     */
    public void out(AttendScheduleWithBLOBs attendScheduleWithBLOBs) {
        if (attendScheduleWithBLOBs != null) {
            //获取当前排班所有的uid
            List<Integer> uidList = this.getUidList(attendScheduleWithBLOBs);
            if (uidList != null && uidList.size() > 0) {
//                删除所有添加类型只为 1-排班添加 的数据
                userDutyMapper.deleteUserDutyByUidList(uidList,1);
//                修改添加类型包含 1 的 数据
                Map map = new HashMap();
                map.put("dateStart",  AttendDateUtil.getNextDate());
                map.put("listUid", uidList);
                map.put("addType", "1");
                userDutyMapper.updateByAddType(map);
            }
        }
    }

    /**
     * 启动（插入相应的排班人员）
     * （1）按照开启时间顺序设置排班
     * （2）按照（用户 -> 角色 -> 部门）优先级设置
     * 注** 排班和调休 按照 设置时间 互相覆盖
     */
    public void start(AttendScheduleWithBLOBs attendScheduleWithBLOBs, Integer type) {
        if (attendScheduleWithBLOBs != null) {
            if (type == 1) {
                this.insertUserDutyList(attendScheduleWithBLOBs);
            } else {

            }
        }
    }

    /**
     * 自由排班查询
     */
    @Override
    public ToJson freedomScheduleQuery(String deptIds, String userIds, String date, Integer pageSize, Integer page, Boolean useFlag) {
        ToJson toJson = new ToJson(1, "err");
        boolean bool = true;
        try {
            List<Users> usersList = new ArrayList<>();
            List<UserDuty> userDutieMain = new ArrayList<>();
            Map<String, Object> maps = new HashMap<>();
//          用户数
            Integer total = 0;
//          分页
            Integer startNum = 0;
            Integer endNum = pageSize;
            if (useFlag) {
                if (page != null && pageSize != null) {
                    if (page != 1) {
                        endNum = page * pageSize + 1;
                        startNum = (page - 1) * pageSize + 1;
                    }
                }
                maps.put("startNum", startNum);
                maps.put("endNum", endNum);
            }
            if (!StringUtils.checkNull(deptIds) && deptIds.length() > 1) {
                String[] deptId = deptIds.split(",");
                maps.put("deptIds", deptId);
                total = attendScheduleMapper.freedomScheduleQueryByDeptCount(deptId);
                usersList = attendScheduleMapper.freedomScheduleQueryByDept(maps);
            } else if (!StringUtils.checkNull(userIds) && userIds.length() > 1) {
                String[] userId = userIds.split(",");
                maps.put("userIds", userId);
                total = attendScheduleMapper.freedomScheduleQueryByUserCount(userId);
                usersList = attendScheduleMapper.freedomScheduleQueryByUser(maps);
            } else {
                toJson.setMsg("无用户数据");
                return toJson;
            }
            if (usersList.size() > 0) {
                //获得此月全部日期
                List<String> monthMaxDay = AttendDateUtil.getMonthMaxDay(date);
                List<Map<String, Object>> weekList = new ArrayList<>();
                //遍历每个用户
                for (Users user : usersList) {
                    List<Map<String, Object>> listMap = new ArrayList<>();
                    UserDuty userDuty1 = new UserDuty();
                    Integer uid = user.getUid();
                    List<UserDuty> userDutyList = userDutyMapper.getUserDutyListByUidAndDates(uid, monthMaxDay.get(0), monthMaxDay.get(monthMaxDay.size() - 1));
                    Map<String, UserDuty> mapDuty = new HashMap();
                    for (UserDuty userDuty : userDutyList) {
                        mapDuty.put(userDuty.getDutyDate(), userDuty);
                    }
                    for (String dangqianDate : monthMaxDay) {
                        Map<String, Object> map = new HashMap<>();
                        String isDianJi = isDate(dangqianDate + " 00:00:00");
                        //如果此人有排班
                        UserDuty userDuty = mapDuty.get(dangqianDate);
                        String asName = null ;
                        if (userDuty != null) {
//                          节假日优先
                            if(userDuty.getAddType().contains("3")){
                                asName = "节假日";
                            }else{
                                AttendSet attendSet = attendSetMapper.selectAttendSetSid(userDuty.getDutyType());
                                if (attendSet != null) {
                                    asName = attendSet.getTitle();
                                    map.put("asId", attendSet.getSid());
//                                    调休上班
                                    if(userDuty.getAddType().equals("2")){
                                        asName = "(调休上班)" + asName;
                                    }
                                } else {
                                    asName = "考勤类型不存在";
                                }
                            }
                        } else {
                            asName = "休息";
                        }
                        map.put("asName", asName);
                        map.put("time", dangqianDate);
                        map.put("isDianJi", isDianJi);
                        listMap.add(map);
//                    获取每个日期是周几
                        String zhouji = "";
                        int week = AttendDateUtil.getWeekByData(dangqianDate);
                        switch (week) {
                            case 1:
                                zhouji = "一";
                                break;
                            case 2:
                                zhouji = "二";
                                break;
                            case 3:
                                zhouji = "三";
                                break;
                            case 4:
                                zhouji = "四";
                                break;
                            case 5:
                                zhouji = "五";
                                break;
                            case 6:
                                zhouji = "六";
                                break;
                            case 7:
                                zhouji = "日";
                                break;
                        }
                        Map<String, Object> weekMap = new HashMap<>();
                        weekMap.put("week", zhouji);
                        weekMap.put("isDianJi", isDianJi);
                        if (bool) {
                            weekList.add(weekMap);
                        }
                    }
                    bool = false;
                    if (listMap.size() > 0) {
                        userDuty1.setMap(listMap);
                        userDuty1.setUserName(usersMapper.getUsernameById(user.getUid()));
                        userDuty1.setUid(user.getUid());
                        userDutieMain.add(userDuty1);
                    }
                }
                toJson.setFlag(0);
                toJson.setMsg("获取成功");
                toJson.setObj(userDutieMain);
                toJson.setObject(weekList);
                toJson.setTotleNum(total);
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }


    /**
     * 判断当前参数日期是否小于等于当前系统日期
     * 小于当前系统时间返回disabled
     */
    public String isDate(String date) {
        Date date1 = DateFormat.getDate(date);
        int result = new Date().compareTo(date1);
        if (result == -1) {
            return "usable";
        } else {
            return "disabled";
        }
    }


    /**
     * 自由排班保存
     */
    @Override
    public ToJson freedomScheduleSave(List<String> userDutyList) {
        ToJson toJson = new ToJson(1, "err");
        try {
            if (userDutyList != null && userDutyList.size() > 0) {
                for (int i = 0; i < userDutyList.size(); i++) {
                    List dateList = new ArrayList();
                    List<UserDuty> values = new ArrayList<>();
                    String userDutyData = userDutyList.get(i);
                    String[] uidAndDateType = userDutyData.split("\\|");
                    String uid = uidAndDateType[0];
                    String[] dateAndTypes = uidAndDateType[1].split("\\.");
                    for (int j = 0; j < dateAndTypes.length; j++) {
                        if (dateAndTypes[j] != null && !dateAndTypes[j].equals("")) {
                            UserDuty userDuty = new UserDuty();
                            String[] dateAndType = dateAndTypes[j].split(":");
                            dateList.add(dateAndType[0]);
                            if (!dateAndType[1].equals("undefined")) {
                                userDuty.setUid(Integer.parseInt(uid));
                                userDuty.setDutyType(Integer.parseInt(dateAndType[1]));
                                userDuty.setDutyDate(dateAndType[0]);
                                userDuty.setAddType("1");
                                values.add(userDuty);
                            }
                        }
                    }
//                    删除数据
                    userDutyMapper.deleteUserDutyByUidAndDate(uid, dateList, "1");
//                    修改数据
                    Map map = new HashMap();
                    map.put("uid", uid);
                    map.put("listDate", dateList);
                    map.put("addType", "1");
                    userDutyMapper.updateByAddType(map);
                    if (values.size() > 0) {
                        userDutyMapper.insertUserDutyByValues(values, 1);
                    }
                }
                toJson.setFlag(0);
                toJson.setMsg("true");
            } else {
                toJson.setMsg("参数为null");
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 保存排班调度(可以修改历史数据)
     */
    @Override
    public ToJson saveUserDuty(AttendScheduleWithBLOBs attendScheduleWithBLOBs) {
        ToJson toJson = new ToJson();
        try {
            if (attendScheduleWithBLOBs != null) {
                attendScheduleWithBLOBs.setUseUsers("ALL_USER");
                List<Integer> uids = this.getUidList(attendScheduleWithBLOBs);
                if (uids != null && uids.size() > 0) {
                    Map map = new HashMap();
                    map.put("listUid", uids);
                    map.put("dateStart", attendScheduleWithBLOBs.getDateStart());
                    map.put("dateEnd", attendScheduleWithBLOBs.getDateEnd());
                    map.put("addType", "1");
//                    删除排班
                    userDutyMapper.deleteUserDutyByUidListAndDate(map);
                    userDutyMapper.updateByAddType(map);
                    if (attendScheduleWithBLOBs.getSetId() != null && attendScheduleWithBLOBs.getSetId() != 0) {
                        List<String> dayList = AttendDateUtil.getDays(attendScheduleWithBLOBs.getDateStart(), attendScheduleWithBLOBs.getDateEnd());
                        List<UserDuty> values = new ArrayList<>();
                        for (Integer uid : uids) {
                            for (String str : dayList) {
                                UserDuty userDuty = new UserDuty();
                                userDuty.setUid(uid);
                                userDuty.setDutyType(attendScheduleWithBLOBs.getSetId());
                                userDuty.setDutyDate(str);
                                userDuty.setAddType("1");
                                values.add(userDuty);
                            }
                        }
                        if (values != null && values.size() > 0) {
                            userDutyMapper.insertUserDutyByValues(values, 1);
                        }
                    }
                }
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 判断是否有冲突
     */
    public boolean isClash(AttendScheduleWithBLOBs attendScheduleWithBLOBs) {
        Set<Integer> restsUidList = new HashSet<>();
        //获取所有启动的排班
        List<AttendScheduleWithBLOBs> attendScheduleWithBLOBsList = attendScheduleMapper.getStatusOneAttendSchedule();
        if (attendScheduleWithBLOBsList != null) {
            for (AttendScheduleWithBLOBs restsAttendScheduleWithBLOBs : attendScheduleWithBLOBsList) {
                if ("ALL_DEPT".equals(restsAttendScheduleWithBLOBs.getDeptId().trim())) {
                    return true;
                }
                List<Integer> uidList1 = this.getUidList(restsAttendScheduleWithBLOBs);
                restsUidList.addAll(uidList1);
            }
        } else {
            return false;
        }
        //获取当前的排班人员
        List<Integer> uidList = this.getUidList(attendScheduleWithBLOBs);
        //判断排班数据人员是否冲突
        if (restsUidList.size() > 0 && uidList.size() > 0) {
            return !Collections.disjoint(restsUidList, uidList);
        }
        return false;
    }

    //获得当前排班的所有uid
    public List<Integer> getUidList(AttendScheduleWithBLOBs attendScheduleWithBLOBs) {
        Set<Integer> uidList = new HashSet<>();
        //获取当前的排班人员
        //部门下
        Map map = new HashMap();
        map.put("userUsers", attendScheduleWithBLOBs.getUseUsers());
        if (!StringUtils.checkNull(attendScheduleWithBLOBs.getDeptId())) {
            if (!attendScheduleWithBLOBs.getDeptId().equals("ALL_DEPT")) {
                map.put("deptIds", attendScheduleWithBLOBs.getDeptId().split(","));
                List<Integer> uids = attendScheduleMapper.getUidListByDeptIds(map);
                uidList.addAll(uids);
            } else {
                List<Integer> uids = attendScheduleMapper.getUidAll(map);
                uidList.addAll(uids);
            }
        }
        //角色下
        if (!StringUtils.checkNull(attendScheduleWithBLOBs.getPrivId())) {
            map.put("privIds", attendScheduleWithBLOBs.getPrivId().split(","));
            List<Integer> uids = attendScheduleMapper.getUidListByPrivIds(map);
            uidList.addAll(uids);
        }
        //userId
        if (!StringUtils.checkNull(attendScheduleWithBLOBs.getUserId())) {
            String[] userids = attendScheduleWithBLOBs.getUserId().split(",");
            List<Integer> uids = attendScheduleMapper.getUidListByUserIds(userids);
            uidList.addAll(uids);
        }
        return new ArrayList<>(uidList);
    }

    /**
     * 根据uid插入用户排班
     */
    public void insertUserDutyList(AttendScheduleWithBLOBs attendScheduleWithBLOBs) {
        List<Integer> uidList = this.getUidList(attendScheduleWithBLOBs);
        if (uidList.size() > 0) {
            //设置排班类型时已过去的时间不可被更改
            Date newdate = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(newdate);
            calendar.add(Calendar.DATE, 1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateStr = sdf.format(calendar.getTime());
            List<String> dayList = AttendDateUtil.getDays(dateStr, attendScheduleWithBLOBs.getDateEnd());
            List<UserDuty> values = new ArrayList<>();
            for (int j = 0; j < uidList.size(); j++) {
                for (String date : dayList) {
                    String type = null;
                    int week = AttendDateUtil.getWeekByData(date);
                    //获取对应星期的
                    if (week != 0) {
                        switch (week) {
                            case 1:
                                type = attendScheduleWithBLOBs.getMondayId();
                                break;
                            case 2:
                                type = attendScheduleWithBLOBs.getTuesdayId();
                                break;
                            case 3:
                                type = attendScheduleWithBLOBs.getWednesdayId();
                                break;
                            case 4:
                                type = attendScheduleWithBLOBs.getThursdayId();
                                break;
                            case 5:
                                type = attendScheduleWithBLOBs.getFridayId();
                                break;
                            case 6:
                                type = attendScheduleWithBLOBs.getSaturdayId();
                                break;
                            case 7:
                                type = attendScheduleWithBLOBs.getSundayId();
                                break;
                        }
                    }
                    if (!StringUtils.checkNull(type) && !type.equals("0")) {
                        UserDuty userDuty = new UserDuty();
                        userDuty.setUid(uidList.get(j));
                        userDuty.setDutyType(Integer.parseInt(type));
                        userDuty.setDutyDate(date);
                        userDuty.setAddType("1");
                        values.add(userDuty);
                    }
                }
            }
            //执行插入操作
            if (values != null && values.size() > 0) {
                userDutyMapper.insertUserDutyByValues(values, 1);
            }
        }
    }

    /**
     * 获取所有班次
     */
    @Override
    public ToJson getAttendSet() {
        ToJson toJson = new ToJson(1, "err");
        try {
            List<AttendSet> attendSetList = attendSetMapper.selectAttendSet();
            if (attendSetList != null) {
                //添加排班时长
                for (AttendSet attendSet : attendSetList) {
                    getDuration(attendSet);
                }
                toJson.setFlag(0);
                toJson.setMsg("成功");
                toJson.setObj(attendSetList);
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return toJson;
    }

    /**
     * 获得总时长
     */
    public void getDuration(AttendSet attendSet) {
        Integer durationSecond = 0;
        if ("0".equals(attendSet.getAttendType())) {
            //第一轮时间差
            String atime1 = attendSet.getAtime1();
            String atime2 = attendSet.getAtime2();
            if (!StringUtils.checkNull(atime1) && !StringUtils.checkNull(atime2)) {
                Integer second = AttendDateUtil.getSecond(atime1, atime2);
                durationSecond = durationSecond + second;
            }
            //第二轮时间差
            String atime3 = attendSet.getAtime3();
            String atime4 = attendSet.getAtime4();
            if (!StringUtils.checkNull(atime3) && !StringUtils.checkNull(atime4)) {
                Integer second = AttendDateUtil.getSecond(atime3, atime4);
                durationSecond = durationSecond + second;
            }
            //第三轮时间差
            String atime5 = attendSet.getAtime5();
            String atime6 = attendSet.getAtime6();
            if (!StringUtils.checkNull(atime5) && !StringUtils.checkNull(atime6)) {
                Integer second = AttendDateUtil.getSecond(atime5, atime6);
                durationSecond = durationSecond + second;
            }
        } else if ("1".equals(attendSet.getAttendType())) {
            Integer workHours = attendSet.getWorkHours();
            durationSecond = workHours != null ? workHours * 60 * 60 : 0;
        }
        attendSet.setDuration(AttendDateUtil.secToTime(durationSecond));
        attendSet.setDurationSeconde(durationSecond);
    }
}
